package com.holyn.guofang.view.Login;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.login.GetPasswordContract;
import com.holyn.guofang.presenter.login.GetPasswordPresent;
import com.holyn.guofang.utils.ValidUtil;
import com.holyn.guofang.view.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/6/8.
 */

public class GetPasswordActivity extends BaseActivity implements BaseView, GetPasswordContract.View{

    private static final int REQUEST_ACTION_FIND_PASSWORD = 0x0001;

    @BindView(R.id.rb_method_phone)
    RadioButton rbMethodPhone;
    @BindView(R.id.rb_method_email)
    RadioButton rbMethodEmail;
    @BindView(R.id.rg_checkMethod)
    RadioGroup rgCheckMethod;
    @BindView(R.id.tv_check_method_title)
    TextView tvCheckMethodTitle;
    @BindView(R.id.et_check_method_value)
    EditText etCheckMethodValue;
    @BindView(R.id.btn_next)
    Button btnNext;

    private GetPasswordContract.Presenter getPasswordPresent;

    private Integer checkMethod = 1;
    private String equipmentCode;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_get_password;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("密码找回");

        getPasswordPresent = new GetPasswordPresent(this);
        getPasswordPresent.addViewListener(this);
        initView();
    }

    private void initView() {
        rgCheckMethod.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_method_phone:
                        tvCheckMethodTitle.setText("手机号码：");
                        etCheckMethodValue.setHint("请输入手机号码");
                        break;
                    case R.id.rb_method_email:
                        tvCheckMethodTitle.setText("电子邮箱：");
                        etCheckMethodValue.setHint("请输入邮箱地址");
                        break;
                }
            }
        });
    }

    @OnClick(R.id.btn_next)
    public void onViewClicked() {
        equipmentCode = etCheckMethodValue.getText().toString();
        if (rbMethodPhone.isChecked()) {
            checkMethod = 1;
            if (!ValidUtil.isTelephone(equipmentCode)) {
                showToastDialog("请输入正确的电话号码");
                return;
            }
        } else {
            checkMethod = 2;
            if (!ValidUtil.isEmail(equipmentCode)) {
                showToastDialog("请输入正确的邮箱");
                return;
            }
        }
        getPasswordPresent.postPasswordCode(checkMethod, equipmentCode, REQUEST_ACTION_FIND_PASSWORD);
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, StatusWo statusWo) {

        if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS) {
            toastMsg("获取验证码成功");
            Intent intent = new Intent(GetPasswordActivity.this, GetPasswordNextActivity.class);
            intent.putExtra("checkMethod", checkMethod);
            intent.putExtra("equipmentCode", equipmentCode);
            startActivity(intent);
            finish();
        } else if (statusWo.getStatus() == HttpConfigure.STATUS_ERROR) {
            toastMsg("获取验证码失败");
        } else if (statusWo.getStatus() == -1) {
            toastMsg("手机号码或电子邮箱不存在");
        } else if (statusWo.getStatus() == -2) {
            toastMsg("验证码不匹配");
        } else if (statusWo.getStatus() == -3) {
            toastMsg("验证次数超过 限制");
        } else if (statusWo.getStatus() == -4) {
            toastMsg("一分钟内请求超过 1 次");
        }
    }
}